package com.iplant.aps.serviceImpl.dao.mrp;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.mrp.MRPMaterialItem;
import com.iplant.aps.service.po.mrp.MRPMaterialPlan;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@DBBaseDAO(MRPMaterialPlan.class)
@Slf4j
public class MRPMaterialPlanDAO extends BaseDAO {

    private static MRPMaterialPlanDAO Instance = null;

    public static MRPMaterialPlanDAO getInstance() {
        if (Instance == null) {
            Instance = new MRPMaterialPlanDAO();
        }
        return Instance;
    }

    public MRPMaterialPlanDAO() {
        super("");
        this.Init();
    }


    public void MRP_UpdateMaterialPlan(BMSEmployee wLoginUser, MRPMaterialPlan wMRPMaterialPlan, OutResult<Integer> wErrorCode) {

        this.UpdatePo(wLoginUser, wMRPMaterialPlan, wErrorCode);
        if (wErrorCode.Result != 0 || wMRPMaterialPlan.ItemList == null)
            return;
        for (MRPMaterialItem mrpMaterialItem : wMRPMaterialPlan.ItemList) {
            mrpMaterialItem.PlanID=wMRPMaterialPlan.ID;
            MRPMaterialItemDAO.getInstance().UpdatePo(wLoginUser, mrpMaterialItem, wErrorCode);
        }
    }

    public void MRP_DeleteMaterialPlanByIDList(BMSEmployee wLoginUser, List<Integer> wIDList, OutResult<Integer> wErrorCode){

        if(wIDList==null||wIDList.size()==0)
            return;

        Map<String, Object> wData = new HashMap<>();

        wData.put("PlanFQTY", 0);

        this.Update(wData, null, StringUtils.Format("AND ID IN ({0})", StringUtils.Join(",", wIDList)));


        wData.put("FQTYDone", 0);
        this.Delete(wData, StringUtils.Format("AND ID IN ({0})", StringUtils.Join(",", wIDList)));

        MRPMaterialItemDAO.getInstance().MRP_DeleteMaterialItemByPlanIDList(wLoginUser, wIDList, wErrorCode);
    }


    public List<MRPMaterialPlan> MRP_SelectMaterialPlanListByDate(BMSEmployee wLoginUser,
                                                                  Calendar wStartDate, Calendar wEndDate, Pagination wPagination, OutResult<Integer> wErrorCode) {
        Map<String, Object> wParams = new HashMap<>();
        wParams.put("StartDate", wStartDate);
        wParams.put("EndDate", wEndDate);

        this.MRP_UpdateMaterialPlanFQTYDone(wLoginUser, wStartDate, wEndDate, wErrorCode);
        if(wErrorCode.Result!=0)
            return new ArrayList<>();

        List<MRPMaterialPlan> wResult = this.SelectPOList(wLoginUser, wParams,wPagination, wErrorCode);

        return wResult;
    }


    /**
     * 自动更新配料数  和荣专属
     * @param wLoginUser
     * @param wStartDate
     * @param wEndDate
     * @param wErrorCode
     */
    public void MRP_UpdateMaterialPlanFQTYDone(BMSEmployee wLoginUser,Calendar wStartDate, Calendar wEndDate,OutResult<Integer> wErrorCode){
        try{


            String wSQLText= StringUtils.Format("update {0} t set t.FQTYDone= " +
                            "( select ifnull( sum(t2.ReceiveQuantity),0) from {1} t2 WHERE t2.FactoryID=t.FactoryID" +
                            " and t.OrderNo=t2.ProjectNo and t.MaterialID=t2.MaterialID  " +
                            " and DATE_FORMAT(t2.CreateTime, ''%Y-%m'') = DATE_FORMAT(t.DemandMonth, ''%Y-%m'') )" +
                            " where  t.ID>0 and t.DemandMonth>= :StartDate and t.DemandMonth< :EndDate   ",
                    this.getTableName(),this.getTableName(DBSchemas.WMS,"wms_receivebillitem"));

            Map<String,Object> wParams=new HashMap<>();
            wParams.put("StartDate", wStartDate);
            wParams.put("EndDate", wEndDate);
            this.Update(wSQLText,wParams);

        }catch (Exception e){
            wErrorCode.Result= ExceptionTypes.DBSQL.getValue();
            log.error("MRP_UpdateMaterialPlanFQTYDone",e);
        }
    }

}
